#!/bin/bash

source "$(
  cd "$(dirname "$0")" || exit 1
  pwd
)/common.sh"

function install_development() {
  sudo yum install -y wget tar Cython expat-devel libX11-devel mesa-libGL-devel \
  zlib zlib-devel gd gd-devel libxml* libopenblas* boost-system boost-filesystem libatomic environment-modules -y
  source /etc/profile
}

function download_fsl() {
  echo "# fsl正在下载"

  wget -t 40 -T 20 -c -O "$1"/fsl-6.0.5.2-sources.tar.gz \
    https://fsl.fmrib.ox.ac.uk/fsldownloads/fsl-6.0.5.2-sources.tar.gz

  if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1m# fsl已经下载成功\033[0m"
  else
    echo -e "\033[31m# fsl下载失败,请重新执行脚本\033[0m"
    exit 1
  fi
}

function install_fsl() {
  echo "# fsl正在安装"
  tar -xzf "$1"/fsl-6.0.5.2-sources.tar.gz -C "$1" && rm -rf "$1"/fsl-6.0.5.2-sources.tar.gz
  export FSLDIR="$1"/fsl
  cd "$1"/fsl/etc/fslconf
  sed -i '52,53s/^#//g' fsl.sh
  sed -i '132s/x86_64/aarch64/' fslpython_install.sh
  sed -i '38d' fslpython_environment.yml
  sed -i '21d' fslpython_environment.yml
  sed -i '17d' fslpython_environment.yml
  sed -i '16d' fslpython_environment.yml
  sed -i '112,122d' configure_eddy.sh 

  cd "$1"/fsl/config
  sed -i '135s/-m64//' buildSettings.mk

  cd "$1"/fsl/extras/src/zlib
  sed -i '14s/CFLAGS="${cflags} -O3 -msse4.2 -mpclmul" CPPFLAGS="${cppflags}" LDFLAGS="${ldflags}" //' fslconfigure 

  cd "$1"/fsl/extras/src/libgd
  sed -i '6s/--without-xpm/--build=aarch64/' fslconfigure 
  sed -i '15s/CFLAGS="${cflags}" LDFLAGS="${ldflags}" CPPFLAGS="${cppflags}"//' fslconfigure 

  cd "$1"/fsl/extras/src/libxmlpp/
  sed -i '13s/--enable-shared=no/--enable-shared=no --build=arm CC=clang CXX=clang++/' fslconfigure 

  cd "$1"/fsl/extras/src/libxmlpp/libxml++/io
  sed -i '42s/input_/input_.good()/' istreamparserinputbuffer.cc
  sed -i '32s/output_/output_.good()/' ostreamoutputbuffer.cc 
  sed -i '39s/output_/output_.good()/' ostreamoutputbuffer.cc 

  cd "$1"/fsl
  sed -i '118d' build
  sed -i '112d' build
  sed -i '92d' build
  sed -i '114d' build
  sed -i '108d' build
  sed -i '96d' build

  cd "$1"/fsl/extras/include/armawrap/tests	
  sed -i '5s/-O3/-O3 -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile

  cd "$1"/fsl/extras/include/boost/tools/build/src/engine/boehm_gc
  sed -i '39s/-DALL_INTERIOR_POINTERS/-DALL_INTERIOR_POINTERS -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.direct
  sed -i '32s/-DNO_EXECUTE_PERMISSION/-DNO_EXECUTE_PERMISSION -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.dj
  sed -i '154s/-DGC_OPERATOR_NEW_ARRAY/-DGC_OPERATOR_NEW_ARRAY -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.dj
  sed -i '283s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '293s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libgd
  sed -i '116s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '11s/-DHAVE_LIBXPM/-DHAVE_LIBXPM -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.sample
  
  cd "$1"/fsl/extras/src/libgd/config
  sed -i '91s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libgd/netware
  sed -i '96s/-nostdinc/-nostdinc -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.netware

  cd "$1"/fsl/extras/src/libgd/netware/tests
  sed -i '88s/-nostdinc/-nostdinc -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.netware

  cd "$1"/fsl/extras/src/libgd/src
  sed -i '261s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libgd/tests
  sed -i '869s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt
  sed -i '281s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '287s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/api
  sed -i '209s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '215s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/auglag
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/bobyqa
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/cdirect
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/cobyla
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/crs
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/direct
  sed -i '176s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '182s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/esch
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/isres
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/luksan
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/mlsl
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/mma
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/neldermead
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/newuoa
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/octave
  sed -i '207s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '213s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/praxis
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/slsqp
  sed -i '175s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '181s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/stogo
  sed -i '194s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '200s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/swig
  sed -i '230s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '236s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libnlopt/util
  sed -i '182s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in
  sed -i '188s/@CXXFLAGS@/@CXXFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libpng
  sed -i '469s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libpng/contrib/gregbook
  sed -i '67s/-DFEATURE_LOOP/-DFEATURE_LOOP -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.unx

  cd "$1"/fsl/extras/src/libpng/scripts
  sed -i '16s/DEFINE=PNG_INTERNAL/DEFINE=PNG_INTERNAL -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.amiga
  sed -i '12s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.atari
  sed -i '41s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.darwin
  sed -i '56s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.dec
  sed -i '16s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.dj2
  sed -i '57s/-O2/-O2 -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.elf
  sed -i '39s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.hp64
  sed -i '56s/-O3/-O3 -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.hpgcc
  sed -i '47s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.hpux
  sed -i '26s/-O2/-O2 -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.ibmc
  sed -i '30s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.knr
  sed -i '25s/-O/-O -march=armv8.2-a -fsigned-char -ffp-contract=on/' makefile.mips

  cd "$1"/fsl/extras/src/libxml2-2.9.2
  sed -i '464s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libxml2-2.9.2/VxWorks
  sed -i '16s/-D_REENTRANT=1/-D_REENTRANT=1 -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile

  cd "$1"/fsl/extras/src/libxml2-2.9.2/doc
  sed -i '218s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libxml2-2.9.2/doc/devhelp
  sed -i '158s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libxml2-2.9.2/doc/examples
  sed -i '293s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libxml2-2.9.2/example
  sed -i '183s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd "$1"/fsl/extras/src/libxml2-2.9.2/include
  sed -i '187s/@CFLAGS@/@CFLAGS@ -march=armv8.2-a -fsigned-char -ffp-contract=on/' Makefile.in

  cd /usr/lib64
  ln -sf libboost_filesystem.so.1.73.0 libboost_filesystem.so
  ln -sf libboost_system.so.1.73.0 libboost_system.so
  source "$1"/fsl/etc/fslconf/fsl.sh

  cd "$1"/fsl
  ./build
  echo -e "\033[1;32;1m# fsl已经安装完毕\033[0m"
}

function mk_fsl() {
  mkdir -p "$2"/fsl-6.0.5.2 && cp -r "$1"/fsl/* "$2"/fsl-6.0.5.2
}

function write_fsl_module() {
  cat >"$1/fsl-6.0.5.2/fsl_modulefiles" <<EOF
#%Module1.0
conflict fsl
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
  variable modfile
  set modfile_path [file dirname \$modfile]
  return \$modfile_path
}
set pwd [getModulefileDir]
set FSL \$pwd
setenv FSL \$FSL
prepend-path PATH \$FSL/bin
EOF
}

function main() {
  set_script_dir
  set -e
  is_check_build_install_directory "$1" "$2" "fsl-6.0.5.2.sh"
  is_check_build_equal "$1" "$2"
  install_development
  download_fsl "$1"
  install_fsl "$1" "$2"
  mk_fsl "$1" "$2"
  write_fsl_module "$2"
  rm -rf "$1"/fsl
}

main "$@"
